﻿// Module:             Games Programming
// Assignment1:        Platformer Game
// Add game feature:   
// Student Number:     S10095741D
// Student Name:       Ng Cheong Sian

//////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Game.UIScripts
{
    class LevelEditor : Panel
    {
        private readonly Level _level;

        private UIController _ui;

        private readonly ContentManager _contentManager;
        private MouseState _previousMouseState;

        private readonly List<UIButton> _buttons = new List<UIButton>();
        private int _selectedButtonIndex = -1;

        public LevelEditor(IServiceProvider serviceProvider, GraphicsDevice graphicsDevice, ContentManager content, UIController controller)
        {
            _contentManager = content;
            _ui = controller;
            _level = new Level(serviceProvider, "test1", graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height);
            LoadContent();
        }

        public void Reset()
        {

        }


        private void LoadContent()
        {
            _buttons.Add(new UIButton("test", new Vector2(0, 50), _contentManager.Load<Texture2D>("Sprites/Platforms/Start"),false));
        }


        public void Update(GameTime gameTime, KeyboardState keyboardState, MouseState mouseState)
        {
            _level.Update(gameTime, keyboardState, mouseState);

            if (mouseState.LeftButton == ButtonState.Pressed && _previousMouseState.LeftButton == ButtonState.Released)
            {
                //When clicked
                int counter = 0;
                foreach(UIButton btn in _buttons)
                {
                    if (checkCollision(new Vector2(mouseState.X, mouseState.Y), btn))
                    {
                        Console.WriteLine("Clicked");

                        if(!btn.IsSpawn)
                        {
                            _buttons.Add(new UIButton(btn.Name, btn.Position, btn.Texture, false));
                            btn.SetIsSpawn(true);
                        }
                        _selectedButtonIndex = counter;
                        _previousMouseState = mouseState;
                        break;
                    }
                    else
                    {
                        counter++;
                    }
                }
            }
            else if (mouseState.LeftButton == ButtonState.Released && _previousMouseState.LeftButton == ButtonState.Pressed)
            {
                //When released
                Console.WriteLine("Released");
                _selectedButtonIndex = -1;
                _previousMouseState = mouseState;
            }
            else if (mouseState.LeftButton == ButtonState.Pressed && _previousMouseState.LeftButton == ButtonState.Pressed)
            {
                Console.WriteLine(_selectedButtonIndex);
                if(_selectedButtonIndex != -1)
                    _buttons[_selectedButtonIndex].SetPosition(new Vector2(mouseState.X - _buttons[_selectedButtonIndex].Texture.Width / 2, mouseState.Y-_buttons[_selectedButtonIndex].Texture.Height / 2));
            }
            
            
        }


        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            _level.Draw(gameTime, spriteBatch);
            //spriteBatch.Draw(_texture);
            spriteBatch.Begin();
            foreach (UIButton btn in _buttons)
            {
                spriteBatch.Draw(btn.Texture, btn.Position, Color.White);
            }
            
            spriteBatch.End();
        }

        private bool checkCollision(Vector2 mousePos, UIButton btn)
        {
            if (mousePos.X >= btn.Position.X // To the right of the left side
            && mousePos.X < btn.Position.X + btn.Texture.Width //To the left of the right side
            && mousePos.Y > btn.Position.Y //Below the top side
            && mousePos.Y < btn.Position.Y + btn.Texture.Height) //Above the bottom side
                return true; //We are; return true.
            else
                return false; //We're not; return false.
        }

    }
}
